home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / libpcap-0.0.6 / savefile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-25  |  8.2 KB  |  312 lines

  1. /*
  2.  * Copyright (c) 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21. #ifndef lint
  22. static char rcsid[] =
  23.     "@(#)$Header: savefile.c,v 1.16 94/06/20 19:07:56 leres Exp $ (LBL)";
  24. #endif
  25.  
  26. /*
  27.  * savefile.c - supports offline use of tcpdump
  28.  *    Extraction/creation by Jeffrey Mogul, DECWRL
  29.  *    Modified by Steve McCanne, LBL.
  30.  *
  31.  * Used to save the received packet headers, after filtering, to
  32.  * a file, and then read them later.
  33.  * The first record in the file contains saved values for the machine
  34.  * dependent values so we can print the dump file on any architecture.
  35.  */
  36.  
  37. #include <sys/types.h>
  38. #include <sys/time.h>
  39.  
  40. #include <net/bpf.h>
  41.  
  42. #include <errno.h>
  43. #include <memory.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <unistd.h>
  47.  
  48. #include "pcap-int.h"
  49.  
  50. #define TCPDUMP_MAGIC 0xa1b2c3d4
  51.  
  52. /*
  53.  * We use the "receiver-makes-right" approach to byte order,
  54.  * because time is at a premium when we are writing the file.
  55.  * In other words, the pcap_file_header and pcap_pkthdr,
  56.  * records are written in host byte order.
  57.  * Note that the packets are always written in network byte order.
  58.  *
  59.  * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
  60.  * machine (if the file was written in little-end order).
  61.  */
  62. #define    SWAPLONG(y) \
  63. ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
  64. #define    SWAPSHORT(y) \
  65.     ( (((y)&0xff)<<8) | (((y)&0xff00)>>8) )
  66.  
  67. #define SFERR_TRUNC        1
  68. #define SFERR_BADVERSION    2
  69. #define SFERR_BADF        3
  70. #define SFERR_EOF        4 /* not really an error, just a status */
  71.  
  72. static int
  73. sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
  74. {
  75.     struct pcap_file_header hdr;
  76.  
  77.     hdr.magic = TCPDUMP_MAGIC;
  78.     hdr.version_major = PCAP_VERSION_MAJOR;
  79.     hdr.version_minor = PCAP_VERSION_MINOR;
  80.  
  81.     hdr.thiszone = thiszone;
  82.     hdr.snaplen = snaplen;
  83.     hdr.sigfigs = 0;
  84.     hdr.linktype = linktype;
  85.  
  86.     if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
  87.         return (-1);
  88.  
  89.     return (0);
  90. }
  91.  
  92. static void
  93. swap_hdr(struct pcap_file_header *hp)
  94. {
  95.     hp->version_major = SWAPSHORT(hp->version_major);
  96.     hp->version_minor = SWAPSHORT(hp->version_minor);
  97.     hp->thiszone = SWAPLONG(hp->thiszone);
  98.     hp->sigfigs = SWAPLONG(hp->sigfigs);
  99.     hp->snaplen = SWAPLONG(hp->snaplen);
  100.     hp->linktype = SWAPLONG(hp->linktype);
  101. }
  102.  
  103. pcap_t *
  104. pcap_open_offline(char *fname, char *errbuf)
  105. {
  106.     register pcap_t *p;
  107.     register FILE *fp;
  108.     struct pcap_file_header hdr;
  109.     int linklen;
  110.  
  111.     p = (pcap_t *)malloc(sizeof(*p));
  112.     if (p == NULL) {
  113.         strcpy(errbuf, "out of swap");
  114.         return (NULL);
  115.     }
  116.  
  117. #ifdef notdef
  118.     bzero(p, sizeof(*p));
  119. #else
  120.     memset(p, 0, sizeof(*p));
  121. #endif
  122.     /*
  123.      * Set this field so we don't close stdin in pcap_close!
  124.      */
  125.     p->fd = -1;
  126.  
  127.     if (fname[0] == '-' && fname[1] == '\0')
  128.         fp = stdin;
  129.     else {
  130.         fp = fopen(fname, "r");
  131.         if (fp == NULL) {
  132.             sprintf(errbuf, "%s: %s", fname, pcap_strerror(errno));
  133.             goto bad;
  134.         }
  135.     }
  136.     if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
  137.         sprintf(errbuf, "fread: %s", pcap_strerror(errno));
  138.         goto bad;
  139.     }
  140.     if (hdr.magic != TCPDUMP_MAGIC) {
  141.         if (SWAPLONG(hdr.magic) != TCPDUMP_MAGIC) {
  142.             sprintf(errbuf, "bad dump file format");
  143.             goto bad;
  144.         }
  145.         p->sf.swapped = 1;
  146.         swap_hdr(&hdr);
  147.     }
  148.     if (hdr.version_major < PCAP_VERSION_MAJOR) {
  149.         sprintf(errbuf, "archaic file format");
  150.         goto bad;
  151.     }
  152.     p->tzoff = hdr.thiszone;
  153.     p->snapshot = hdr.snaplen;
  154.     p->linktype = hdr.linktype;
  155.     p->sf.rfile = fp;
  156.     p->bufsize = hdr.snaplen;
  157.     /* Align link header as required for proper data alignment */
  158.     linklen = 14;                    /* XXX */
  159.     p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
  160.     p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
  161.     p->sf.version_major = hdr.version_major;
  162.     p->sf.version_minor = hdr.version_minor;
  163.  
  164.     return (p);
  165.  bad:
  166.     free(p);
  167.     return (NULL);
  168. }
  169.  
  170. /*
  171.  * Read sf_readfile and return the next packet.  Return the header in hdr
  172.  * and the contents in buf.  Return 0 on success, SFERR_EOF if there were
  173.  * no more packets, and SFERR_TRUNC if a partial packet was encountered.
  174.  */
  175. static int
  176. sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, int buflen)
  177. {
  178.     FILE *fp = p->sf.rfile;
  179.  
  180.     /* read the stamp */
  181.     if (fread((char *)hdr, sizeof(struct pcap_pkthdr), 1, fp) != 1) {
  182.         /* probably an EOF, though could be a truncated packet */
  183.         return (1);
  184.     }
  185.  
  186.     if (p->sf.swapped) {
  187.         /* these were written in opposite byte order */
  188.         hdr->caplen = SWAPLONG(hdr->caplen);
  189.         hdr->len = SWAPLONG(hdr->len);
  190.         hdr->ts.tv_sec = SWAPLONG(hdr->ts.tv_sec);
  191.         hdr->ts.tv_usec = SWAPLONG(hdr->ts.tv_usec);
  192.     }
  193.     /*
  194.      * We interchanged the caplen and len fields at version 2.3,
  195.      * in order to match the bpf header layout.  But unfortunately
  196.      * some files were written with version 2.3 in their headers
  197.      * but without the interchanged fields.
  198.      */
  199.     if (p->sf.version_minor < 3 ||
  200.         (p->sf.version_minor == 3 && hdr->caplen > hdr->len)) {
  201.         int t = hdr->caplen;
  202.         hdr->caplen = hdr->len;
  203.         hdr->len = t;
  204.     }
  205.  
  206.     if (hdr->caplen > buflen) {
  207.         /*
  208.          * This can happen due to Solaris 2.3 systems tripping
  209.          * over the BUFMOD problem and not setting the snapshot
  210.          * correctly in the savefile header.  If the caplen isn't
  211.          * grossly wrong, try to salvage.
  212.          */
  213.         static u_char *tp = NULL;
  214.         static int tsize = 0;
  215.  
  216.         if (tsize < hdr->caplen) {
  217.             tsize = ((hdr->caplen + 1023) / 1024) * 1024;
  218.             if (tp != NULL)
  219.                 free((u_char *)tp);
  220.             tp = (u_char *)malloc(tsize);
  221.             if (tp == NULL) {
  222.                 sprintf(p->errbuf, "BUFMOD hack malloc");
  223.                 return (-1);
  224.             }
  225.         }
  226.         if (fread((char *)tp, hdr->caplen, 1, fp) != 1) {
  227.             sprintf(p->errbuf, "truncated dump file");
  228.             return (-1);
  229.         }
  230.         memcpy((char *)buf, (char *)tp, buflen);
  231.  
  232.     } else {
  233.         /* read the packet itself */
  234.  
  235.         if (fread((char *)buf, hdr->caplen, 1, fp) != 1) {
  236.             sprintf(p->errbuf, "truncated dump file");
  237.             return (-1);
  238.         }
  239.     }
  240.     return (0);
  241. }
  242.  
  243. /*
  244.  * Print out packets stored in the file initialized by sf_read_init().
  245.  * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
  246.  */
  247. int
  248. pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  249. {
  250.     struct bpf_insn *fcode = p->fcode.bf_insns;
  251.     int status = 0;
  252.     int n = 0;
  253.  
  254.     while (status == 0) {
  255.         struct pcap_pkthdr h;
  256.  
  257.         status = sf_next_packet(p, &h, p->buffer, p->bufsize);
  258.         if (status) {
  259.             if (status == 1)
  260.                 return (0);
  261.             return (status);
  262.         }
  263.  
  264.         if (fcode == NULL ||
  265.             bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
  266.             (*callback)(user, &h, p->buffer);
  267.             if (++n >= cnt && cnt > 0)
  268.                 break;
  269.         }
  270.     }
  271.     /*XXX this breaks semantics tcpslice expects */
  272.     return (n);
  273. }
  274.  
  275. /*
  276.  * Output a packet to the initialized dump file.
  277.  */
  278. void
  279. pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
  280. {
  281.     FILE * f = (FILE *)user;
  282.     (void)fwrite((char *)h, sizeof(*h), 1, f);
  283.     (void)fwrite((char *)sp, h->caplen, 1, f);
  284. }
  285.  
  286. /*
  287.  * Initialize so that sf_write() will output to the file named 'fname'.
  288.  */
  289. pcap_dumper_t *
  290. pcap_dump_open(pcap_t *p, char *fname)
  291. {
  292.     FILE *f;
  293.     if (fname[0] == '-' && fname[1] == '\0')
  294.         f = stdout;
  295.     else {
  296.         f = fopen(fname, "w");
  297.         if (f == NULL) {
  298.             sprintf(p->errbuf, "%s: %s",
  299.                 fname, pcap_strerror(errno));
  300.             return (NULL);
  301.         }
  302.     }
  303.     (void)sf_write_header(f, p->linktype, p->tzoff, p->snapshot);
  304.     return ((pcap_dumper_t *)f);
  305. }
  306.  
  307. void
  308. pcap_dump_close(pcap_dumper_t *p)
  309. {
  310.     fclose((FILE *)p);
  311. }
  312.